//
//  ZSHomeController.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/6.
//  Copyright © 2016年 ichange. All rights reserved.
//





import UIKit

class ZSHomeController: ZSBaseViewController {
    
   private lazy var indicator : UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        
        indicator.startAnimating()
        
        return indicator
        
    }()
    
    private lazy var refresh:ZSRefresh = ZSRefresh()
    
    var homeListViewModel = ZSHomeListViewModel()
    
    //保存数据的数组
    var dataList:[ZSStatus] = {
        
        let s = [ZSStatus]()
        
        return s
    }()
    
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        tableView.separatorStyle = .None
        tableView.backgroundColor = UIColor.grayColor()
        
        refresh.backgroundColor = UIColor.yellowColor()
        //实例化一下自带的刷新控件
        tableView.addSubview(refresh)
        
        
        refresh.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(tableView)
            make.height.equalTo(64)
            make.width.equalTo(screenWidth)
            make.top.equalTo(tableView).offset(-64)
        }
        
        refresh.addTarget(self, action: "performNewStatus", forControlEvents: .ValueChanged)
        
        visitorView?.setVisitorViewInfo("visitordiscover_feed_image_house", text: "关注一些人，回这里看看有什么惊喜",isHome: true)
        
        if userViewModel.userLogin{
            
            navigationItem.leftBarButtonItem = UIBarButtonItem.creatItem("navigationbar_friendsearch", target: self, action: "leftItemClick")
            
            navigationItem.rightBarButtonItem = UIBarButtonItem.creatItem("navigationbar_pop", target: self, action: "rightItemClick")
            
            print("登录了")
            registerCell()
            
            tableView.rowHeight = 250
            
            tableView.rowHeight = UITableViewAutomaticDimension
            
            tableView.estimatedRowHeight = 250
            
            tableView.tableFooterView = indicator
            
            
            /**
            *  加载微博数据
            */
            homeListViewModel.loadHomeData({ (isSuccess, result) -> () in
               
                //只是给刷新控件赋一个值
                self.refresh.endRefreshing()
                
                if isSuccess{
                    
                    self.dataList = result
                    self.tableView.reloadData()
                }else{
                
                    print("网络错误")
                    return
                }
            })
            
            
            
        }
            
        else{
            print("没登录")
            
        }
        
        
    }
    
    //MARK:注册cell
    func registerCell(){
        
        tableView.registerClass(ZSStatusCell.self, forCellReuseIdentifier: "statusCell")
    }
    
    func leftItemClick(){
        navigationController?.pushViewController(ZSTestViewController(), animated: true)
    }
    
    func rightItemClick(){
        navigationController?.pushViewController(ZSTestViewController(), animated: true)
    }
}



















//MARK: 这个方法中实现数据源方法
extension ZSHomeController{
    
    //MARK:返回多少行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return (dataList.count)
    }
    
    //MARK:每行返回怎样的cell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ZSStatusCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("statusCell", forIndexPath: indexPath) as! ZSStatusCell
        
        cell.selectionStyle = .None
        
        cell.statusViewModel = ZSStatusViewModel(status: self.dataList[indexPath.row])
        
        
        //判断是不是最后一个cell-->如果是最后一个cell
        if indexPath.row == dataList.count-1{
            //加载更多数据
            //cell.statusViewModel?.status?.id--->网络工具类拿到这个id去加载id 比这个更小的 status
            
            homeListViewModel.loadMoreHomeData((cell.statusViewModel?.status?.id)!-1, finished: { (isSuccess, result) -> () in
                
                if isSuccess{
                    self.dataList.appendContentsOf(result)
                    tableView.reloadData()
                }
                else{
                    print("加载失败")
                }
            })
        }
        return cell
    }
    
    
    
    
    
    
    
    
    //此方法中实现 下拉刷新,并且展示新数据
    func performNewStatus(){

        
        //没网的情况下
        if dataList.count == 0{
            print("网络错误")
            self.refresh.endRefreshing()
            return
        }
        
        
        homeListViewModel.loadNewHomeData((dataList.first?.id)!) { (isSuccess, result) -> () in

            self.refresh.endRefreshing()

            if isSuccess{

                //没有加载到新的数据
                if result.count==0{
                    self.refreshControl?.endRefreshing()
                    return
                }

                self.dataList = result

                self.tableView.reloadData()
                
                self.refreshControl?.endRefreshing()
            }
            
            else{
                self.refreshControl?.endRefreshing()
                print("网络错误")
                
                return

            }
        }
    }
    
    
    
    
    
    
}









